package com.anhuanjia.tools.eao.util;

import java.util.HashMap;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.anhuanjia.tools.eao.config.GoogleMapConfig;
import com.anhuanjia.tools.eao.config.GoogleMapKeyConfig;
import com.efastsoft.framework.http.util.ServletUtil;
import com.efastsoft.framework.utility.StringUtil;

/**
 * 获取经纬度
 *
 * @author jueyue 返回格式：Map<String,Object> map map.put("status",
 *         reader.nextString());//状态 map.put("result", list);//查询结果
 *         list<map<String,String>> 密钥:f247cdb592eb43ebac6ccd27f796e2d2
 */
public class GetLatAndLng {
	/**
	 *
	 * 高德地图地址获取经纬度
	 *
	 * @param @param address
	 * @param @return
	 * @param @throws Exception
	 * @return Map<String,Double>
	 * @throws @author ltj
	 * @date 2017年9月20日下午8:46:52
	 */
//	public static Map<String, Double> getLngAndLatByAmap(String address) throws Exception {
//		address = address.trim();
//		Map<String, Double> map = new HashMap<String, Double>();
//		String url = "https://maps.google.com/maps/api/geocode/geo?address=上海市&sensor=false&key=AIzaSyBoEKbKeeXybVFUqSJLczN9ZCNM4_RmERc";
//		GetMethod method = new GetMethod(url);
//		method.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
//		HttpClient client = new HttpClient();
//		client.getHttpConnectionManager().getParams().setConnectionTimeout(10000); // 设置连接超时
//		int status = client.executeMethod(method);
//		if (status == 200) {
//			String json = method.getResponseBodyAsString();
//			System.out.println(json);
//			JSONObject obj = JSONObject.parseObject(json);
//			if (obj.get("status").toString().equals("1")) {
//				JSONArray array = obj.getJSONArray("geocodes");
//				String str = array.getString(0);
//				JSONObject locationjson = JSONObject.parseObject(str);
//				str = locationjson.getString("location");
//				String[] location = str.split(",");
//				double lng = Double.parseDouble(location[0]);
//				double lat = Double.parseDouble(location[1]);
//				map.put("lng", lng);
//				map.put("lat", lat);
//				System.out.println("经度：" + lng + "---纬度：" + lat);
//			} else {
//				System.out.println("未找到相匹配的经纬度！");
//				throw new Exception();
//			}
//		}
//		return map;
//	}

	public static Map<String, Object> getGoogleLatLng(String address) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtil.isNotEmpty(address)) {
			String responseStr = ServletUtil.getURLContent(GoogleMapConfig.getValue() + "?address=" + address
					+ "&output=json&ak=" + GoogleMapKeyConfig.getValue(), "UTF-8");
//			String responseStr = ServletUtil.getURLContent("http://api.map.baidu.com/geocoder/v2/" + "?address=" + address
//					+ "&output=json&ak=" + "LVf1osslOvyXyglNCsg2ULK9otcY6P2o", "UTF-8");
			JSONObject response = JSON.parseObject(responseStr);
			if (!StringUtils.isEmpty(response)) {
				JSONObject o2 = (JSONObject) response.get("result");
				if (!StringUtils.isEmpty(o2)) {
					JSONObject o5 = (JSONObject) o2.get("location");
					map.put("lng", o5.get("lng"));
					map.put("lat", o5.get("lat"));
				} else {
					map.put("lng", 39.9054);
					map.put("lat", 116.3976);
				}
			}
			return map;
		} else {
			map.put("lng", 39.9054);
			map.put("lat", 116.3976);
			return map;
		}
	}
	public static void main(String[] args) throws Exception {
		Map<String, Object> map = getGoogleLatLng("河南省新乡市商会大厦");
		System.out.println(map);
	}
}
